package com.store.service.impl;

import com.store.entity.*;
import com.store.mapper.AddressMapper;
import com.store.mapper.CartMapper;
import com.store.mapper.OrderItemMapper;
import com.store.mapper.OrderMapper;
import com.store.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.store.service.ex.InsertException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.management.remote.JMXAddressable;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cai
 * @since 2022-07-21
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {


    @Autowired
    AddressMapper addressMapper;
    @Autowired
    CartMapper cartMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Transactional
    @Override
    public Order addOrder(Integer aid, Integer[] cids,String username) {
        //


        //地址信息
        Address address= addressMapper.selectById(aid);
        //购物车信息
        List<SuperCart> list = cartMapper.selectSuperCart(cids);
        LocalDateTime now= LocalDateTime.now();

        Order order=new Order();
        //封装地址信息
        order.setUid(address.getUid());
        order.setRecvName(address.getName());
        order.setRecvPhone(address.getPhone());
        order.setRecvProvince(address.getProvinceName());
        order.setRecvCity(address.getCityName());
        order.setRecvArea(address.getAreaName());
        order.setRecvAddress(address.getAddress());
        //封装商品信息
        //计算总价格
        long allPrice=0;
        for (SuperCart cart:list){
            allPrice+=cart.getPrice()*cart.getNum();
        }
        order.setTotalPrice(allPrice);
        order.setStatus(0);
        order.setOrderTime(now);
        //创建日志信息
        order.setCreatedUser(username);
        order.setCreatedTime(now);
        order.setModifiedUser(username);
        order.setModifiedTime(now);


        //
        System.out.println(order);



        //添加到数据库
        Integer rows= orderMapper.insert(order);
        if (rows!=1){
            throw new InsertException("创建订单失败");
        }

        //
        //-------------------------------------------------------------------------------
        //

        // 遍历carts，循环插入订单商品数据
        for (SuperCart cart : list) {
            // 创建订单商品数据
            OrderItem item = new OrderItem();
            // 补全数据：setOid(order.getOid())
            item.setOid(order.getOid());
            // 补全数据：pid, title, image, price, num
            item.setPid(cart.getPid());
            item.setTitle(cart.getTitle());
            item.setImage(cart.getImage());
            item.setPrice(cart.getRealPrice());
            item.setNum(cart.getNum());
            // 补全数据：4项日志
            item.setCreatedUser(username);
            item.setCreatedTime(now);
            item.setModifiedUser(username);
            item.setModifiedTime(now);

            //
            System.out.println(item);




            // 插入订单商品数据
            Integer rows2 =orderItemMapper.insert(item);
            if (rows2 != 1) {
                throw new InsertException("插入订单商品数据时出现未知错误，请联系系统管理员");
            }
        }

















        return order;
    }
}
